-
Notifications
You must be signed in to change notification settings - Fork 551
Fix get_class return type
#4456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.1.x
Are you sure you want to change the base?
Conversation
42e1755 to
fa22c84
Compare
ondrejmirtes
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like a proper solution would be a new Type method. Basically the opposite of getClassStringObjectType. That one goes from class-string<Foo> to Foo.
We need to go from object Foo to class-string<Foo>.
And personally I don't think there's anything wrong with class-string<object{foo: Bar}> because it'd allow for a precise type when doing new on it again.
1ce9e04 to
5702d17
Compare
5702d17 to
d30f64a
Compare
I introduced |
|
This pull request has been marked as ready for review. |
d30f64a to
076ef63
Compare
076ef63 to
eac96db
Compare
| */ | ||
| public function getObjectClassReflections(): array; | ||
|
|
||
| public function getClassStringType(): Type; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to document what this is supposed to return similarly to how getClassStringObjectType and getObjectTypeOrClassStringObjectType are documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added Return class-string<Foo> for object type Foo. ; is it enough ?
caf99fb to
b4af2a3
Compare
| public function getClassStringType(): Type | ||
| { | ||
| return new GenericClassStringType(new ObjectType($this->getClassName())); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from my experience enum-case can generate a separate class of bugs, therefore I think we should have a separate test for it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand which test you expect since the bug was about class with hasProperty and an enum-case cannot have a property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but it could have a method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might be wrong but since an enum is final method_exists calls will be either always true or always false on it and the HasMethodType won't be added.
I'm getting
enum MyEnum
{
case CASE1;
case CASE2;
public function someMethod(): bool { return true; }
}
class HelloWorld
{
public function withEnumCase(): void
{
$entity = MyEnum::CASE1;
assertType('class-string<Bug4890PHP8\MyEnum>', get_class($entity));
assert(method_exists($entity, 'someMethod'));
assertType('class-string<Bug4890PHP8\MyEnum>', get_class($entity));
}
}
Do you have a test in mind ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about
enum MyEnum
{
case CASE1;
case CASE2;
public function someMethod(): bool { return true; }
}
class HelloWorld
{
public function withEnumCase(UnitEnum $entity): void
{
assertType('class-string<UnitEnum>', get_class($entity));
assert(method_exists($entity, 'someMethod'));
assertType('class-string<UnitEnum&hasMethod<someMethod>>', get_class($entity));
}
}
$h = new HelloWorld();
$h->withEnumCase(MyEnum::CASE1);| public function getClassStringType(): Type | ||
| { | ||
| return new GenericClassStringType($this); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might also be worth testing with templates
b4af2a3 to
0fb0665
Compare
Closes #2350
Closes phpstan/phpstan#4890